package com.kele.service.impl;

import com.kele.base.core.BaseServiceImpl;
import com.kele.model.bo.StudentBO;
import com.kele.model.entity.QStudent;
import com.kele.model.entity.Student;
import com.kele.model.vo.StudentVO;
import com.kele.repository.StudentRepository;
import com.kele.service.IStudentService;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Objects;

@Service
public class StudentServiceImpl extends BaseServiceImpl<Student, Long, StudentBO, StudentVO> implements IStudentService {

    private final StudentRepository studentRepository;

    public StudentServiceImpl(StudentRepository studentRepository) {
        super(studentRepository);
        this.studentRepository = studentRepository;
    }

    @Override
    public Predicate buildQueryPredicate(StudentBO bo) {
        QStudent student = QStudent.student;
        BooleanBuilder builder = new BooleanBuilder();
        if (StringUtils.hasText(bo.getName())){
            builder.and(student.name.like("%" + bo.getName() + "%"));
        }
        if (Objects.nonNull(bo.getAge())){
            builder.and(student.age.eq(bo.getAge()));
        }
        return builder;
    }
}
